//
//  GKNewFeatureViewController.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/12.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class GKNewFeatureViewController: UICollectionViewController {

    let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(){
    
        super.init(collectionViewLayout: layout)
        
        //设置layout属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(GKNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return newFeatureCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GKNewFeatureCell
        
        cell.index = indexPath.item
        
        return cell
    }
    
}

extension GKNewFeatureViewController {
    
    //scrollview停止滚动的时候调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int( scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width )
        
        if index == newFeatureCount - 1
        {   //此时完全滚动到了最后一页并停止
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! GKNewFeatureCell
            cell.btnAnimate()
        }
        
    }
}







//在一个文件中添加多个类
class GKNewFeatureCell: UICollectionViewCell {
    
    //当前cell 的页码
    var index: Int  = 0 {
        
        didSet{
            let imgName = "new_feature_\(index + 1)"
            GKPrint(imgName)
            bgView.image = UIImage(named: imgName)
            
            //隐藏按钮
            self.btnStart.hidden = true
        }
    }
    
    //按钮动画效果方法  外界调用
    func btnAnimate (){
        //显示按钮
        btnStart.hidden = false
        btnStart.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {
            //清空按钮的缩放动画: 使它从小变到大
            self.btnStart.transform = CGAffineTransformIdentity
            }, completion: nil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    //准备UI
    private func prepareUI(){
        //添加到子控件
        contentView.addSubview(bgView)
        contentView.addSubview(btnStart)
        //添加约束
        bgView.snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        btnStart.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    //点击开始体验按钮
    @objc private func didClickStartBtn(){
        
        let vc = GKTabBarController()
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(vc)
    }
    
    
    //懒加载
    private lazy var bgView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var btnStart: UIButton = {
       
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(didClickStartBtn), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
}














